<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      data = [
        {
          id: 1,
          name: '主管1',
          children: [
            {
              id: 11,
              name: '经理11'
            },
            {
              id: 12,
              name: '经理12',
              children: [
                {
                  id: 121,
                  name: '经理121'
                }
              ]
            }
          ]
        },
        {
          id: 2,
          name: '主管2',
          children: [
            {
              id: 21,
              name: '经理21'
            },
            {
              id: 22,
              name: '经理22'
            }
          ]
        }
      ]
      // 需求遍历所有的id
      function getAllId(data, arr) {
        for (var i = 0; i < data.length; i++) {
          if (!data[i].children) {
            //  如果没有孩子
            arr.push(data[i].id)
          } else {
            // 有孩子
            arr.push(data[i].id)
            getAllId(data[i].children, arr)
          }
        }
        return arr
      }
      var arr = []
      console.log(getAllId(data, arr))
    </script>
  </body>
</html>
